package com.company.堆栈队列;

import java.util.Stack;

/**
 * @author Administrator
 */
public class 包含min函数的栈 {

    Stack<Integer> stack = new Stack<>();
    Stack<Integer> minStack = new Stack<>();

    public void push(int node) {
        stack.push(node);
        if (minStack.isEmpty() || node <= stack.peek()) {
            minStack.push(stack.peek());
        } else {
            minStack.push(stack.peek());
        }
    }

    public void pop() {
        stack.pop();
        minStack.pop();
    }

    public int top() {
        if (!stack.isEmpty()) {
            return stack.peek();
        }
        return 0;
    }

    public int min() {
        int min = stack.get(stack.toArray().length - 1);
        for (int i = 0; i < stack.toArray().length; i++) {
            Integer res = stack.get(i);
            if (res < min) {
                min = res;
            }
        }
        return min;
    }
}
